package com.controller.base;

import com.bean.base.DictInfo;
import com.bean.base.DictItemInfo;
import com.bean.other.MetaInfo;
import com.bean.other.RequestData;
import com.bean.other.ResponseData;
import com.github.pagehelper.PageInfo;
import com.service.impl.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("api/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @PostMapping("addDict")
    public ResponseEntity<ResponseData> add(@RequestBody DictInfo dictInfo){
        String msg=dictService.insertDict(dictInfo);
        if(msg.contains("成功")){
            return ResponseEntity.ok(ResponseData.success("成功"));
        }else{
            return ResponseEntity.ok(ResponseData.error(msg));
        }

    }

    @PostMapping("editDict")
    public ResponseEntity<ResponseData> edit(@RequestBody DictInfo dictInfo){
        dictService.updateDict(dictInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @DeleteMapping("delDict/{id}")
    public ResponseEntity<ResponseData> del(@PathVariable String id){
        DictInfo dictInfo=new DictInfo();
        dictInfo.setId(id);
        dictService.deleteDict(dictInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @PostMapping("listDict")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> list(@RequestBody RequestData<DictInfo> requestData){
        DictInfo dictInfo=requestData.getObj();
        MetaInfo metaInfo =requestData.getMetaInfo();
        PageInfo<DictInfo> pageInfo=dictService.selectDict(dictInfo,metaInfo);
        metaInfo.setPages(pageInfo.getPages());
        metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo.getList(),metaInfo));
    }


    @PostMapping("addDictItem")
    public ResponseEntity<ResponseData> addItem(@RequestBody DictItemInfo dictItemInfo){
        String msg=dictService.insertDictItem(dictItemInfo);
        if(msg.contains("成功")){
            return ResponseEntity.ok(ResponseData.success("成功"));
        }else{
            return ResponseEntity.ok(ResponseData.error(msg));
        }

    }

    @PostMapping("editDictItem")
    public ResponseEntity<ResponseData> editItem(@RequestBody DictItemInfo dictItemInfo){
        dictService.updateDictItem(dictItemInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @DeleteMapping("delDictItem/{id}")
    public ResponseEntity<ResponseData> delItem(@PathVariable String id){
        DictItemInfo dictItemInfo=new DictItemInfo();
        dictItemInfo.setId(id);
        dictService.deleteDictItem(dictItemInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @PostMapping("listDictItem")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> listItem(@RequestBody RequestData<DictItemInfo> requestData){
        DictItemInfo dictItemInfo=requestData.getObj();
        MetaInfo metaInfo =requestData.getMetaInfo();
        PageInfo<DictItemInfo> pageInfo=dictService.selectDictItem(dictItemInfo,metaInfo);
        metaInfo.setPages(pageInfo.getPages());
        metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo.getList(),metaInfo));
    }

    @PostMapping("listItem")
    public ResponseEntity<ResponseData> listItem(@RequestBody DictItemInfo dictItemInfo){
       List<DictItemInfo> list=dictService.selectDictItem(dictItemInfo);
        return ResponseEntity.ok(ResponseData.success(list));
    }

}
